package com.cuixichang.minimalism.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuixichang.minimalism.core.mybatis.cnstants.BaseFieldNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import com.cuixichang.minimalism.mapper.IRoleMenuMapper;
import com.cuixichang.minimalism.transfer.dto.IRoleMenuDTO;
import com.cuixichang.minimalism.transfer.factory.IRoleMenuAssembler;
import com.cuixichang.minimalism.transfer.po.IRoleMenu;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.function.Function;
import org.springframework.stereotype.Service;

/**
 * 菜单角色绑定关系表表服务实现类
 *
 * @author cuixichang
 * @date 2024-12-18
 */
@Service
public class IRoleMenuServiceImpl implements IRoleMenuService {

    @Autowired
    private IRoleMenuMapper iRoleMenuMapper;

    @Override
    public IRoleMenuDTO selectById(Long id){
        IRoleMenu iRoleMenu = iRoleMenuMapper.selectById(id);
        return IRoleMenuAssembler.INSTANCT.convertDto(iRoleMenu);
    }

    @Override
    public IRoleMenuDTO selectOne(IRoleMenuDTO iRoleMenuDTO){
        Assert.notNull(iRoleMenuDTO,"参数为空,请检查");
        IRoleMenu iRoleMenu = IRoleMenuAssembler.INSTANCT.convertPo(iRoleMenuDTO);
        IRoleMenu result = iRoleMenuMapper.selectOne(new QueryWrapper<>(iRoleMenu));
        return IRoleMenuAssembler.INSTANCT.convertDto(result);
    }

    @Override
    public List<IRoleMenuDTO> selectByIds(List<Long> ids) {
        Assert.notEmpty(ids,"参数为空,请检查");
        List<IRoleMenu> results = iRoleMenuMapper.selectBatchIds(ids);
        return results.stream().map(IRoleMenuAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public List<IRoleMenuDTO> selectList(IRoleMenuDTO iRoleMenuDTO){
        return this.selectList(iRoleMenuDTO, (iRoleMenu, queryWrapper) -> {});
    }

    @Override
    public List<IRoleMenuDTO> selectList(IRoleMenuDTO iRoleMenuDTO, BiConsumer<IRoleMenu, QueryWrapper<IRoleMenu>> consumer){
        Assert.notNull(iRoleMenuDTO,"参数为空,请检查");
        IRoleMenu iRoleMenu = IRoleMenuAssembler.INSTANCT.convertPo(iRoleMenuDTO);
        QueryWrapper<IRoleMenu> queryWrapper = new QueryWrapper<>(iRoleMenu);
        if(Objects.nonNull(consumer)){
            consumer.accept(iRoleMenu, queryWrapper);
        }
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        List<IRoleMenu> results = iRoleMenuMapper.selectList(queryWrapper);
        return results.stream().map(IRoleMenuAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public Page<IRoleMenuDTO> selectPage(IRoleMenuDTO iRoleMenuDTO,long pageSize,long pageNo){
        return this.selectPage(iRoleMenuDTO, pageSize, pageNo, (iRoleMenu, queryWrapper) -> {});
    }

    @Override
    public Page<IRoleMenuDTO> selectPage(IRoleMenuDTO iRoleMenuDTO,long pageSize,long pageNo, BiConsumer<IRoleMenu, QueryWrapper<IRoleMenu>> consumer){
        Assert.notNull(iRoleMenuDTO,"参数为空,请检查");
        IRoleMenu iRoleMenu = IRoleMenuAssembler.INSTANCT.convertPo(iRoleMenuDTO);
        QueryWrapper<IRoleMenu> queryWrapper = new QueryWrapper<>(iRoleMenu);
        if(Objects.nonNull(consumer)) consumer.accept(iRoleMenu, queryWrapper);
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        Page<IRoleMenu> page = iRoleMenuMapper.selectPage(new Page<>(pageNo,pageSize),queryWrapper);
        Page<IRoleMenuDTO> result = Page.of(page.getCurrent(),page.getSize(),page.getTotal());
        result.setRecords(page.getRecords().stream()
                .map(IRoleMenuAssembler.INSTANCT::convertDto).collect(Collectors.toList()));
        return result;
    }

    @Override
    public int deleteById(Long id){
        return iRoleMenuMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(List<Long> ids){
        return iRoleMenuMapper.deleteBatchIds(ids);
    }

    @Override
    public int deleteByCondition(IRoleMenuDTO iRoleMenuDTO){
        return deleteByCondition(iRoleMenuDTO, (iRoleMenu, queryWrapper)-> {});
    }

    @Override
    public int deleteByCondition(IRoleMenuDTO iRoleMenuDTO, BiConsumer<IRoleMenu, QueryWrapper<IRoleMenu>> consumer){
        Assert.notNull(iRoleMenuDTO,"参数为空,请检查");
        IRoleMenu iRoleMenu = IRoleMenuAssembler.INSTANCT.convertPo(iRoleMenuDTO);
        QueryWrapper<IRoleMenu> queryWrapper = new QueryWrapper<>(iRoleMenu);
        if(Objects.nonNull(consumer)) {
            consumer.accept(iRoleMenu, queryWrapper);
        }
        return iRoleMenuMapper.delete(queryWrapper);
    }

    @Override
    public IRoleMenuDTO insert(IRoleMenuDTO iRoleMenuDTO){
        return insertHasOption(iRoleMenuDTO, (IRoleMenuDTO) -> null);
    }

    @Override
    public IRoleMenuDTO insertHasOption(IRoleMenuDTO iRoleMenuDTO, Function<IRoleMenuDTO,IRoleMenuDTO> preOption){
        Assert.notNull(iRoleMenuDTO,"参数为空,请检查");
        iRoleMenuDTO.check();
        IRoleMenu iRoleMenu = IRoleMenuAssembler.INSTANCT.convertPo(iRoleMenuDTO);
        Long createLoginId = iRoleMenu.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iRoleMenu.setUpdateLoginId(createLoginId);
        LocalDateTime localDateTime = LocalDateTime.now();
        iRoleMenu.setCreateTime(localDateTime);
        iRoleMenu.setUpdateTime(localDateTime);
        Boolean isEnable = iRoleMenu.getIsEnable();
        if(Objects.isNull(isEnable)) iRoleMenu.setIsEnable(true);
        Integer sort = iRoleMenu.getSort();
        if(Objects.isNull(sort)) iRoleMenu.setSort(0);
        if(Objects.nonNull(preOption)){
            IRoleMenuDTO apply = preOption.apply(iRoleMenuDTO);
            if(Objects.nonNull(apply)) {
              return apply;
            }
        }
        int insert = iRoleMenuMapper.insert(iRoleMenu);
        Assert.isTrue(insert>0,"插入失败");
        return IRoleMenuAssembler.INSTANCT.convertDto(iRoleMenu);
    }

    @Override
    public int updateById(IRoleMenuDTO iRoleMenuDTO){
         return updateById(iRoleMenuDTO, (iRoleMenu, updateWrapper) -> {});
    }

    @Override
    public int updateById(IRoleMenuDTO iRoleMenuDTO, BiConsumer<IRoleMenu, UpdateWrapper<IRoleMenu>> consumer){
        Assert.notNull(iRoleMenuDTO,"参数为空,请检查");
        Long id = iRoleMenuDTO.getId();
        Assert.notNull(id,"主键为空,请检查");
        IRoleMenu iRoleMenu = IRoleMenuAssembler.INSTANCT.convertPo(iRoleMenuDTO);
        Long createLoginId = iRoleMenu.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iRoleMenu.setCreateLoginId(null);
        iRoleMenu.setUpdateLoginId(createLoginId);
        iRoleMenu.setUpdateTime(LocalDateTime.now());
        UpdateWrapper<IRoleMenu> updateWrapper = new UpdateWrapper<IRoleMenu>().eq(BaseFieldNames.ID, id);
        if(Objects.nonNull(consumer)) consumer.accept(iRoleMenu,updateWrapper);
        return iRoleMenuMapper.update(iRoleMenu,updateWrapper);
    }
}
